import {Platform, ToastController, App} from "ionic-angular";
/**
 *
 * 双击返回退出app
 * 
 */
export abstract class MyBaseApp {
  backButtonPressed: boolean = false;

  constructor(public platform: Platform,
              public app: App,
              public toast: ToastController) {
    this.platform.ready().then(() => {
      this.initializeMyApp();
    });
  }

  initializeMyApp() {
      
    this.platform.registerBackButtonAction((): any => {
      let nav = this.app.getActiveNav();
      if (nav.canGoBack()) {
        nav.pop();
      } else {
        this.showExit();
      }
    });
  }

  showExit() {
    if (this.backButtonPressed) {
      this.exitApp();
    } else {
      let toast = this.toast.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'bottom'
      });
      toast.present();
      this.backButtonPressed = true;
      setTimeout(() => {
        this.backButtonPressed = false;
      }, 2000);
    }
  }

  abstract exitApp();

}

